package com.hrabun.goodmorningalarm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.preference.Preference;
import android.preference.PreferenceManager;

public class Global {
	
	/* Preferences*/
	final static String PREF_RADIO_TIMEOUT = "radioTimeout";
	final static String PREF_24_MODE = "is24modeClock";
	final static String PREF_SUNDAY_STARTING = "isWeekStartsWithSunday";
	final static String PREF_VOLUME_INCREASING = "isVolumeIncreasing";
	
	
	final static String LOG_TAG = "MY";
	final static String DATA_TIME_ATTR = "time";
	final static String DATA_PIC_ATTR = "picture";
	final static String DATA_ID_ATTR = "id";
	final static String DATA_ACTIVE_ATTR = "active";
	
	/*DATABASE*/
	
	final static String DB_NAME = "Alarms";
	final static String DB_TABLE_STATIONS_NAME = "RadioStations";
	final static String DB_TABLE_NAME = "AlarmItems";
	final static int DB_ID_INDEX = 0;
	final static int DB_ALARM_NAME_INDEX = 1;
	final static int DB_ALARM_TIME_INDEX = 2;
	final static int DB_MONDAY_INDEX = 3;
	final static int DB_TUESDAY_INDEX = 4;
	final static int DB_WEDNESDAY_INDEX = 5;
	final static int DB_THURSDAY_INDEX = 6;
	final static int DB_FRIDAY_INDEX = 7;
	final static int DB_SATURDAY_INDEX = 8;
	final static int DB_SUNDAY_INDEX = 9;
	final static int DB_ACTIVE_INDEX = 10;
	final static int DB_ALARM_ITEM_RADIO_ID = 11;
	
	/* Columns*/
	/* Alarms */
	final static String DB_COLUMN_ALARM_ID = "_id";
	final static String DB_COLUMN_ALARM_NAME = "AlarmName";
	final static String DB_COLUMN_ALARM_TIME = "AlarmTime";
	final static String DB_COLUMN_ALARM_ITEM_MONDAY = "Mon";
	final static String DB_COLUMN_ALARM_ITEM_TUESDAY = "Tue";
	final static String DB_COLUMN_ALARM_ITEM_WEDNESDAY = "Wed";
	final static String DB_COLUMN_ALARM_ITEM_THURSDAY = "Thur";
	final static String DB_COLUMN_ALARM_ITEM_FRIDAY = "Fri";
	final static String DB_COLUMN_ALARM_ITEM_SATURDAY = "Sat";
	final static String DB_COLUMN_ALARM_ITEM_SUNDAY = "Sun";
	final static String DB_COLUMN_ACTIVE = "Active";
	final static String DB_COLUMN_ALARM_ITEM_RADIO = "radio";
	
	/* Radio Stations */
	final static String DB_COLUMN_RADIO_STATION_ID = "_id";
	final static String DB_COLUMN_RADIO_STATION_NAME = "name";
	final static String DB_COLUMN_RADIO_STATION_URL = "url";
	final static int DB_RADIO_STATION_ID_INDEX = 0;
	final static int DB_RADIO_STATION_NAME_INDEX = 1;
	final static int DB_RADIO_STATION_URL_INDEX = 2;
	
	final static String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
	final static int DIALOG_TIME_ADD = 1;
	final static int DIALOG_TIME_CHANGE = 2;

	public static String convertFromDateToString (Date aDate) {
		SimpleDateFormat  dateFormat = new SimpleDateFormat(DATETIME_FORMAT, Locale.US);
		String datetime = dateFormat.format(aDate);
		return datetime;
	}
	
	public static Date convertFromStringToDate (String aDate) {
		SimpleDateFormat  format = new SimpleDateFormat(DATETIME_FORMAT, Locale.US); 
		Date date = null;
		try {  
		    date = format.parse(aDate);  
		} catch (ParseException e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}
		return date;
	}
	
	public static String getTimeFromDate(Date aDate) {
		SimpleDateFormat  dateFormat = null;
		if (Global.pref_is24Mode()) {
			dateFormat = new SimpleDateFormat("HH:mm", Locale.US);
		} else {
			dateFormat = new SimpleDateFormat("hh:mm aa", Locale.US);
		}
		return dateFormat.format(aDate);
	}
	
	public static Boolean getStateFromString(String state) {
		if (state == null) return false;
		boolean result = false;
		if (state.equals("1")) {
			result = true;
		}
		
		return result;
	}
	
	public static Typeface setFontRoboto() {
		return Typeface.createFromAsset(MainActivity.getAppContext().getAssets(), "fonts/Roboto-Medium.ttf");
	}
	
	public static Typeface setFontRibeye() {
		return Typeface.createFromAsset(MainActivity.getAppContext().getAssets(), "fonts/Ribeye-Regular.ttf");
	}
	
	public static int pref_getRadioTimeoutPreference() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getAppContext());
		return Integer.valueOf(pref.getString(Global.PREF_RADIO_TIMEOUT, "1"));
	}
	
	public static boolean pref_is24Mode() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getAppContext());
		return pref.getBoolean(Global.PREF_24_MODE, false);
	}
	
	public static boolean pref_isVolumeIncreasing() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getAppContext());
		return pref.getBoolean(Global.PREF_VOLUME_INCREASING, true);
	}
	
	public static boolean pref_isSundayStarting() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getAppContext());
		return pref.getBoolean(Global.PREF_SUNDAY_STARTING, true);
	}
	
	static class Test {
		public static void testTest() {
			
		}
	}

}
